<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ActiveRecord
 *
 * @author Chris Sim
 */
class ActiveRecord extends CActiveRecord{
    //put your code here
    public function validateArray($validateRef) {
        $validate = true;
        if($validateRef  instanceof CActiveRecord)
            $validateRef = array($validateRef);
        foreach($validateRef as $_validateRef) {
            foreach($_validateRef as $ref) {
                if(!$ref->validate()) {
                    Yii::log("Cargo validating error", 'error', 'application.models.'.$ref);
                    $validate = false;
                }
            }
        }
        return $validate;
    }
    public function save2Many($model,$noempty=array(),$foreignID=array()) {
        
        if(!$this->isNewRecord) {
            foreach($model as $item) {
                foreach($foreignID as $k=>$v)
                    $item->$k = $this->$v;
                $found = true;
                foreach($noempty as $_empty) {
                    if(empty($item->$_empty))
                        $found = false;
                }
                if(!empty($item->id) && $item->findByPk($item->id))
                {
                    $item->isNewRecord = false;
                }
                if($found) {
                    if(!$item->save())
                        return false;
                }
            }
        }
        return true;
    }
}
?>
